package cn.charlotte.pit.util.bossbar;

import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.bossbar.BossBar.Color;
import net.kyori.adventure.bossbar.BossBar.Overlay;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class PitBossBar {

    private BossBar bossBar = null;
    private final Set<UUID> players = new HashSet<>();
    private String title;

    public PitBossBar(String title) {
        this.title = title;
    }

    public void addPlayer(Player player) {
        if (players.contains(player.getUniqueId())) {
            return;
        }
        if (bossBar != null) {
            player.showBossBar(bossBar);
            players.add(player.getUniqueId());
        }
    }

    public void removePlayer(Player player) {
        if (players.remove(player.getUniqueId())) {
            player.hideBossBar(bossBar);
        }
    }

    public void setProgress(double progress) {
        // 限制 0.0f ~ 1.0f 范围
        if (bossBar != null) {
            bossBar.progress(Math.max(0f, Math.min((float) progress, 1.0f)));
        }
    }

    public void setTitle(String title) {
        if (title != null && !title.isBlank()) {
            if (this.bossBar != null) {
                bossBar.name(Component.text(title));
            } else {
                this.bossBar = BossBar.bossBar(Component.text(title), 1.0f, Color.PURPLE, Overlay.PROGRESS);
            }

            for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
                onlinePlayer.showBossBar(this.bossBar);
            }
        } else {
            for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
                if (this.bossBar != null) {
                    onlinePlayer.hideBossBar(this.bossBar);
                }
            }
        }
    }

    public String getTitle() {
        if (bossBar != null) {
            return bossBar.name().toString(); // 需要解析 Component（可用 Adventure 的组件转字符串工具）
        }
        return "";
    }

    public void updateTitle(String title) {
        Component newTitle = Component.text(title);
        if (!bossBar.name().equals(newTitle)) {
            bossBar.name(newTitle);
        }
    }

    public void clear() {
        for (UUID uuid : players) {
            Player player = Bukkit.getPlayer(uuid);
            if (player != null && player.isOnline()) {
                player.hideBossBar(bossBar);
            }
        }
        players.clear();
    }

    public Set<UUID> getPlayers() {
        return players;
    }

    public BossBar getBossBar() {
        return bossBar;
    }

    public void setColor(Color color) {
        bossBar.color(color);
    }

    public void setOverlay(Overlay overlay) {
        bossBar.overlay(overlay);
    }
}
